The jparseargs.tcl library is distributed as part of the jstools package. It consists of procedures to procedure and application arguments in the typical Tcl `-option value' format.
This document describes jparseargs.tcl version 3.6/3.0.
Usage
Accessing the Library
In order to use the jparseargs.tcl library, it (and any other libraries it depends on) must be in your Tcl auto_path, described in tclvars(n). Information about how to arrange that, and other conventions common to the jstools libraries, is in the Usage section of The jstools Libraries.
Credits and Copyright
Author
Jay Sekora
js@bu.edu
http://shore.net/~js/
Copyright
The library is copyright ⌐ 1992-1994 by Jay Sekora, but may be freely copied and modified for non¡commercial purposes. (Please contact me if you want to use it for a commercial purpose, this may be OK under some circumstances.)
Overview
Procedures
j:parse_args - parse option list in parent procedure
j:parse_argv - parse argv variable (options to script)
j:parse_args
Usage
proc procname { args } {
j:parse_args arglist
...}
Argument
arglist - list of {option default} pairs
Example
proc alert { args } {
j:parse_args {
{title "Alert"}
{text "Alert!"}
{ok "OK"}
}
toplevel .foo
wm title .foo $title
message .foo.msg -text $text
button .foo.btn -text $ok -command {destroy .foo}
pack .foo.msg .foo.btn
}
Description
This procedure is used by almost all the other procedures in jlibrary.tcl. It extracts options from the variable args in the parent procedure, and sets corresponding variables, also in the parent procedure. (See the proc(1) manual page for an explanation of the args variable.) Its argument is a list of {option default} sublists. For each pair, the parent's args variable is scanned for elements matching option, preceded by a hyphen. (For instance, if an option is `title', j:parse_args searches for `-title'.) If a match is found, the following word is used as the value of that parameter, and a variable with the name option is set to that value in the parent procedure. (To continue the example, the parent's variable title would be set to the element following `-title' in $args.) If no match is found, the parent's option variable is set to the value of default.
Although not as flexible as getopt(3), this procedure makes it fairly easy to process lists of optional arguments.
j:parse_argv
Usage
j:parse_args arglist
Argument
arglist - list of {option default} pairs
Example
j:parse_argv {
{font Courier10}
{headerfont Times-Bold12}
{columns 1}
{orient portrait}
}
Description
This procedure is almost identical to j:parse_args, but instead of parsing the args variable in a procedure, it parses the global argv variable. I.e., instead of parsing procedure arguments, it parses script arguments.
It removes any items it processes from argv, and sets argc to the length of argv when it's done.
For more information about the argv and argc variables, see the wish(1) or tclsh(1) manual page.
Evolution
Feel free to report bugs (and feature requests) to me, <js@bu.edu>, and I will try to deal with them. Also, feel free to fix bugs or add features on your own and let me know how you did it.
Changes Since Version 3.6/2.0
* The j:parse_argv procedure is new.
Changes from Previous Versions
* These procedures used to be in a file called jlibrary.tcl. Starting with version 3.6/2.0, they're in their own independent library.
Future Directions
* There should be a way to make aliases for existing options, in the same way that -bd is an alias for -borderwidth in Tk.
* There should be a way to specify boolean options which don't take an argument, but set a variable to 1 if they appear, and 0 otherwise.